package com.zs.demo.controller.system;

import com.zs.demo.common.log.SysLogAop;
import com.zs.demo.shiro.MyToken;
import com.zs.demo.common.response.IResponseStatus;
import com.zs.demo.pojo.dto.system.login.LoginDTO;
import com.zs.demo.pojo.vo.system.SysUserVO;
import com.zs.demo.service.system.SysUserService;
import com.zs.demo.status.LoginStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/* @Desc: 登陆控制器
 * @author:zhengs  
 * @Time: 2018/11/16 15:53
 * @Copyright: © zhengs 版权所有  
 * @Warning: 本内容仅限于本公司内部传阅,禁止外泄或用于其它商业目的
 */
@RestController
@RequestMapping(value = {"/account"})
@Api(tags = {"登陆"})
public class LoginController {

    @Autowired
    private SysUserService userService;

    @PostMapping(value = {"/sign-in"})
    @ApiOperation(value = "登录")
    @SysLogAop("登录")
    public String signIn(@RequestBody @Validated @ApiParam(value = "登录数据",required = true)
                                         LoginDTO loginDTO){
        userService.signIn(loginDTO);
        return ((MyToken)SecurityUtils.getSubject().getPrincipal()).getToken();
    }

    @PostMapping(value = "/current")
    @ApiOperation(value = "获取当前用户信息")
    @SysLogAop("获取当前用户信息")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public SysUserVO current(){
        return userService.getCurrentUser();
    }

    @PostMapping(value = "/logout")
    @ApiOperation(value = "注销登录")
    @SysLogAop("注销登录")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public IResponseStatus logout(){
        try {
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
        }catch (Exception e){
            return LoginStatus.LOGOUT_FAIL;
        }
        return LoginStatus.LOGOUT_OK;
    }

    @PostMapping(value = "/all-permission-tag")
    @ApiOperation(value = "获取所有的权限标示")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public List<String> getAllPermissionTag(){
        return userService.getAllPermissionTag();
    }
}